import 'package:flutter/material.dart';
import '../models/file_item.dart';

class FileListItem extends StatelessWidget {
  final FileItem file;
  final VoidCallback onTap;
  final VoidCallback onDelete;

  const FileListItem({
    Key? key,
    required this.file,
    required this.onTap,
    required this.onDelete,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Card(
      margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
      child: ListTile(
        leading: _getFileIcon(),
        title: Text(
          file.name,
          style: const TextStyle(fontWeight: FontWeight.w500),
        ),
        subtitle: Row(
          children: [
            if (!file.isFolder) ...[
              Text(file.sizeFormatted),
              const SizedBox(width: 8),
              const Text('•'),
              const SizedBox(width: 8),
            ],
            Text(_formatDateTime(file.uploadedAt)),
          ],
        ),
        trailing: PopupMenuButton(
          itemBuilder: (context) => [
            if (!file.isFolder)
              const PopupMenuItem(
                value: 'download',
                child: Row(
                  children: [
                    Icon(Icons.download),
                    SizedBox(width: 8),
                    Text('下载'),
                  ],
                ),
              ),
            const PopupMenuItem(
              value: 'rename',
              child: Row(
                children: [
                  Icon(Icons.edit),
                  SizedBox(width: 8),
                  Text('重命名'),
                ],
              ),
            ),
            PopupMenuItem(
              value: 'delete',
              child: Row(
                children: [
                  Icon(Icons.delete, color: Colors.red[600]),
                  const SizedBox(width: 8),
                  Text('删除', style: TextStyle(color: Colors.red[600])),
                ],
              ),
            ),
          ],
          onSelected: (value) {
            switch (value) {
              case 'download':
                // TODO: Implement download
                ScaffoldMessenger.of(context).showSnackBar(
                  const SnackBar(content: Text('下载功能即将推出')),
                );
                break;
              case 'rename':
                // TODO: Implement rename
                ScaffoldMessenger.of(context).showSnackBar(
                  const SnackBar(content: Text('重命名功能即将推出')),
                );
                break;
              case 'delete':
                onDelete();
                break;
            }
          },
        ),
        onTap: onTap,
      ),
    );
  }

  Widget _getFileIcon() {
    if (file.isFolder) {
      return Container(
        width: 40,
        height: 40,
        decoration: BoxDecoration(
          color: Colors.blue.withOpacity(0.1),
          borderRadius: BorderRadius.circular(8),
        ),
        child: const Icon(
          Icons.folder,
          color: Colors.blue,
          size: 24,
        ),
      );
    }

    IconData iconData;
    Color iconColor;

    if (file.isImage) {
      iconData = Icons.image;
      iconColor = Colors.green;
    } else if (file.isVideo) {
      iconData = Icons.video_file;
      iconColor = Colors.red;
    } else if (file.isAudio) {
      iconData = Icons.audio_file;
      iconColor = Colors.orange;
    } else if (file.isDocument) {
      iconData = Icons.description;
      iconColor = Colors.blue;
    } else {
      iconData = Icons.insert_drive_file;
      iconColor = Colors.grey;
    }

    return Container(
      width: 40,
      height: 40,
      decoration: BoxDecoration(
        color: iconColor.withOpacity(0.1),
        borderRadius: BorderRadius.circular(8),
      ),
      child: Icon(
        iconData,
        color: iconColor,
        size: 24,
      ),
    );
  }

  String _formatDateTime(DateTime dateTime) {
    final now = DateTime.now();
    final difference = now.difference(dateTime);

    if (difference.inDays > 0) {
      return '${difference.inDays}天前';
    } else if (difference.inHours > 0) {
      return '${difference.inHours}小时前';
    } else if (difference.inMinutes > 0) {
      return '${difference.inMinutes}分钟前';
    } else {
      return '刚刚';
    }
  }
}